package haxe.util.benchmark; 

class BenchmarkArray extends BenchmarkHelper, implements Benchmark
{
	
	var a:Array<TestData>;
	
	public function new( iterations:Int ) 
	{
		super( iterations );
		a = new Array<TestData>();
	}
	
	private function runPush():Void
	{
		a = new Array<TestData>();
		this.init( "Array.push["+iterations+"]" );
		for ( i in 0...iterations )
			a.push( new TestData("test"+i,i) );
		this.done();
	}
	
	private function runUnshift():Void
	{
		a = new Array<TestData>();
		this.init( "Array.unshift["+iterations+"]" );
		for ( i in 0...iterations )
			a.unshift( new TestData("test"+i,i) );
		this.done();
	}
	
	private function runIterate():Void
	{
		a = new Array<TestData>();
		for ( i in 0...iterations )
			a.push( new TestData("test"+i,i ) );
		this.init( "Array.iterate["+iterations+"]" );
		for ( i in a ){i;}
		this.done();
	}
	
	private function runRemove():Void
	{
		var tmp = new Array<TestData>();
		a = new Array<TestData>();
		var rand:Float;
		for ( i in 0...iterations )
		{
			rand = Math.random();
			a.push( new TestData( "test"+rand, rand ) );
			if ( i % 5 == 0 )
				tmp.push( new TestData( "test"+rand, rand ) );
		}
		init( "Array.remove["+(iterations/5)+" from "+iterations+"]" );
		for ( i in tmp )
		{
			a.remove(i);
		}
		done();
	}
	
	private function runPop():Void
	{
		a = new Array<TestData>();
		for ( i in 0...iterations )
			a.push( new TestData( "test"+i, i ) );
		init("Array.pop["+iterations+"]");
		while( a.length > 0 )
			a.pop();
		done();
	}
	
	private function runShift():Void
	{
		a = new Array<TestData>();
		for ( i in 0...iterations )
			a.push( new TestData( "test"+i, i ) );
		init("Array.shift["+iterations+"]");
		while( a.length > 0 )
			a.shift();
		done();
	}
}